#include "expr.h"


void Expression::write(GraphStream * s)
{
	this->accept(s);


	fprintf(s->stream,"\tnode_%d [label=\"Expr node\\n%s\" shape=\"rectangle\"]\n", this->number(),c_type.c_str());
	
	if (c_identiefer!=NULL){
		c_identiefer->write(s);
		fprintf(s->stream,"\tnode_%d -> node_%d [label=\"identiefer\"]\n",this->number(),c_identiefer->number());
	}
	if (c_type.compare("message")==0){
		c_message->write(s);
		fprintf(s->stream,"\tnode_%d -> node_%d [label=\"message\"]\n",this->number(),c_message->number());
	}
	if (c_constant!=0){
		c_constant->write(s);
		fprintf(s->stream,"\tnode_%d -> node_%d [label=\"constant\"]\n",this->number(),c_constant->number());
	}
	if (c_left!=NULL) {
		c_left->write(s);
		fprintf(s->stream,"\tnode_%d -> node_%d [label=\"left\"]\n",this->number(),c_left->number());
	}
	if (c_right!=NULL) {
		c_right->write(s);
		fprintf(s->stream,"\tnode_%d -> node_%d [label=\"right\"]\n",this->number(),c_right->number());
	} 
}

Expression::~Expression()
{
	if (c_identiefer!=NULL){
		delete c_identiefer;
	}
	if (c_left!=NULL) {
		delete c_left;
	}
	if (c_right!=NULL) {
		delete c_right;
	} 
	if (c_type.compare("constant")==0) {
		delete c_constant;
	}
}

Expression::Expression (std::string type, class Expression * left, class Expression * right) : AbstractNode()
{
	c_type=type;
	c_identiefer=NULL;
	c_constant=NULL;
	c_left=left;
	c_right=right;
	c_isNil=false;
	c_message=NULL;
}

Expression::Expression (Id * identiefer) : AbstractNode()
{
	c_type="id";
	c_identiefer=identiefer;
	c_constant=NULL;
	c_left=NULL;
	c_right=NULL;
	c_isNil=false;
	c_message=NULL;
}

Expression::Expression (class Constant * constant) : AbstractNode()
{
	c_type="constant";
	c_identiefer=NULL;
	c_constant=constant;
	c_left=NULL;
	c_right=NULL;
	c_isNil=false;
	c_message=NULL;
}

Expression::Expression (bool isNil) : AbstractNode()
{
	c_type="nil";
	c_identiefer=NULL;
	c_constant=NULL;
	c_left=NULL;
	c_right=NULL;
	c_isNil=true;
	c_message=NULL;
}

Expression::Expression (class Message * message) : AbstractNode()
{
	c_type="message";
	c_identiefer=NULL;
	c_constant=NULL;
	c_left=NULL;
	c_right=NULL;
	c_isNil=false;
	c_message=message;
}

Expression::Expression (class Expression * expr) : AbstractNode()
{
	c_type="return";
	c_identiefer=NULL;
	c_constant=NULL;
	c_left=expr;
	c_right=NULL;
	c_isNil=false;
	c_message=NULL;
}

const std::string & Expression::type()
{
	return c_type;
}